﻿namespace Huayou.Common.Database
{
    using System;

    public class DBOperatorFactory
    {
        public const int Excel = 3;
        public const int MSACCESS = 2;
        public const int MSSQLSERVER = 1;

        public static DBOperate GetDBOperator(string strConnection)
        {
            return GetDBOperator(strConnection, DataBaseTYPE.MSSQLSERVER);
        }

        public static DBOperate GetDBOperator(string strConnection, DataBaseTYPE dataBaseType)
        {
            try
            {
                if (!strConnection.Equals((string) null) && !strConnection.Equals(""))
                {
                    switch (dataBaseType)
                    {
                        case DataBaseTYPE.MSSQLSERVER:
                            return new SqlDBOperate(strConnection);

                        case DataBaseTYPE.MSACCESS:
                            return new OleDBOperate(strConnection);

                        case DataBaseTYPE.MSEXCEL:
                            return new ODBCDBOperate(strConnection);
                    }
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public static DBOperate GetDBOperator(string strConnection, int iDBType)
        {
            try
            {
                if (!strConnection.Equals((string) null) && !strConnection.Equals(""))
                {
                    switch (iDBType)
                    {
                        case 1:
                            return new SqlDBOperate(strConnection);

                        case 2:
                            return new OleDBOperate(strConnection);

                        case 3:
                            return new ODBCDBOperate(strConnection);
                    }
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        public static DBOperate GetDBOperator(string strDBServer, string strDBName, string strDBUser, string strDBPwd, int iDBType)
        {
            string strConnection = "";
            switch (iDBType)
            {
                case 1:
                    strConnection = "data source=" + strDBServer + ";initial catalog=" + strDBName + ";persist security info=False;user id=" + strDBUser + ";password=" + strDBPwd + ";packet size=4096;Pooling=false";
                    break;

                case 2:
                    strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Password='" + strDBPwd + "';User ID=" + strDBUser + ";Data Source=" + strDBName + ";Mode=Share Deny None;Extended Properties='';Jet OLEDB:System database='';Jet OLEDB:Registry Path='';Jet OLEDB:Database Password='';Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password='';Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False";
                    break;

                case 3:
                    strConnection = "Driver={Microsoft Excel Driver (*.xls)};DBQ=" + strDBName + ";";
                    break;

                default:
                    strConnection = "";
                    break;
            }
            return GetDBOperator(strConnection, iDBType);
        }

        public enum DataBaseTYPE
        {
            MSACCESS = 2,
            MSEXCEL = 3,
            MSSQLSERVER = 1
        }
    }
}

